/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import model.Costumer;
import model.Dapcomposition;
import model.Dapstructure;
import model.Dapversion;
import model.Decision;
import model.Department;
import model.Docversion;
import model.Employee;
import model.Product;
import model.Seller;
import model.Stepvalidation;
import model.Tosell;
import model.TosellId;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.ProjetHibernateUtil;

/**
 *
 * @author ISIS
 */
/**
 * store the hibernate session for the seller core
 */
public class SellerCore {

    private static Session session;

    public SellerCore() {
        session = ProjetHibernateUtil.getSessionFactory().openSession();
    }

    public List<Dapstructure> getAllDapstructure() {
        Query query = session.getNamedQuery("HQL_ALL_DAPSTRUCTURE");
        List result = query.list();
        return result;
    }

    public Dapstructure getCurrentDapStructure() {
        for (Dapstructure structure : this.getAllDapstructure()) {
            if (structure.getDscurrent()) {
                return structure;
            }
        }
        return null;
    }
/**
 * create a DAP
 * @param hash
 * @param customer
 * @param title
 * @param seller 
 */
    public void createDAP(HashMap<Product, Integer> hash, Costumer customer, String title, Seller seller) {

        Dapversion dapversion = createDAPVersion(hash, customer, title, seller);

        for (Product entry : hash.keySet()) {
            System.out.println("dvid:" + dapversion.getDvid() + " PID: " + entry.getPid());
            TosellId tosellId = new TosellId(dapversion.getDvid(), entry.getPid());
            Tosell tosell = new Tosell(tosellId, entry, dapversion, (int) hash.get(entry));
            Transaction tr = session.beginTransaction();
            session.saveOrUpdate(tosell);
            tr.commit();
        }
        createFirstDecision(dapversion);
    }
/**
 * get all Step from Database
 * @return 
 */
    public List<Dapcomposition> getAllSteps() {
        Query query = session.getNamedQuery("HQL_ALL_DAPCOMPOSITION");
        List result = query.list();
        return result;
    }
/**
 * create decision for the first step of a DAP version
 * @param dapversion 
 */
    public void createFirstDecision(Dapversion dapversion) {
        Dapstructure structure = getCurrentDapStructure();
        List<Dapcomposition> dapcompo = getAllSteps();
        for (Dapcomposition step : dapcompo) {
            if (step.getDapstructure().equals(structure)) {
                if (step.getSteporder() == 1) {
                    Decision decision = new Decision(dapversion, step.getStepvalidationBySvid(), "In progress");
                    Transaction tr = session.beginTransaction();
                    session.saveOrUpdate(decision);
                    tr.commit();
                }
            }
        }
    }
/**
 * Create a Seller
 * @return 
 */
    public Seller createTestSeller() {
        String sname = "regt";
        Seller seller = new Seller(sname, sname, sname, sname, sname, null, null);
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(seller);
        tr.commit();
        return seller;
    }
/**
 * Create a Customer
 * @param name
 * @param fname
 * @param country
 * @return 
 */
    public Costumer createCustomer(String name, String fname, String country) {
        Costumer customer = new Costumer(name, fname, country);
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(customer);
        tr.commit();
        return customer;
    }
/**
 * create a DAP version
 * @param hash
 * @param customer
 * @param title
 * @param seller
 * @return 
 */
    public Dapversion createDAPVersion(HashMap<Product, Integer> hash, Costumer customer, String title, Seller seller) {
        Dapstructure structure = getCurrentDapStructure();
        System.out.println("Structure:" + structure);
        Dapversion dapversion = new Dapversion(seller, customer, structure, title, false, null, null);
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(dapversion);
        tr.commit();
        return dapversion;
    }
/**
 * get all products from database
 * @return 
 */
    public List<Product> getAllProducts() {
        Query query = session.getNamedQuery("HQL_ALL_PRODUCTS");
        List result = query.list();
        return result;
    }
/**
 * get product by its id
 * @param pid
 * @return 
 */
    public Product getProductbyID(int pid) {
        for (Product prod : this.getAllProducts()) {
            if (prod.getPid().equals(pid)) {
                return prod;
            }
        }
        return null;
    }
/**
 * get product by its description
 * @param pdescription
 * @return 
 */
    public Product getProductbyName(String pdescription) {
        for (Product prod : this.getAllProducts()) {
            if (prod.getPdescription().equals(pdescription)) {
                return prod;
            }
        }
        return null;
    }
/**
 * get all Sellers from database
 * @return 
 */
    public List<Seller> getAllSellers() {
        Query query = session.getNamedQuery("HQL_ALL_SELLER");
        List result = query.list();
        return result;
    }
/**
 * get Seller by its id
 * @param sid
 * @return 
 */
    public Seller getSellerbyID(int sid) {
        for (Seller seller : this.getAllSellers()) {
            if (seller.getSid().equals(sid)) {
                return seller;
            }
        }
        return null;
    }
/**
 * get all Customers from database
 * @return 
 */
    public List<Costumer> getAllCostumers() {
        Query query = session.getNamedQuery("HQL_ALL_CUSTOMERS");
        List result = query.list();
        return result;
    }
/**
 * get customer by its name
 * @param cname
 * @return 
 */
    public Costumer getCostumerbyName(String cname) {
        for (Costumer customer : this.getAllCostumers()) {
            if (customer.getCname().equals(cname)) {
                return customer;
            }
        }
        return null;
    }
/**
 * get all DAP version from database
 * @return 
 */
    public List<Dapversion> getAllDaps() {
        Query query = session.getNamedQuery("HQL_ALL_DAPVERSION");
        List result = query.list();
        return result;
    }
/**
 * get validated DAP version
 * @param seller
 * @return 
 */
    public ArrayList<Dapversion> validatedDAPs(Seller seller) {
        ArrayList<Dapversion> daps = new ArrayList<Dapversion>();
        for (Dapversion dap : getAllDaps()) {
            if (dap.getSeller().equals(seller)) {
                if (dap.getDvfinalvalidation() == true) {
                    daps.add(dap);
                }
            }
        }
        return daps;
    }
/**
 * get non validated DAP version
 * @param seller
 * @return 
 */
    public ArrayList<Dapversion> dAPsInProgress(Seller seller) {
        ArrayList<Dapversion> daps = new ArrayList<Dapversion>();
        for (Dapversion dap : getAllDaps()) {
            if (dap.getSeller().equals(seller)) {
                if (!dap.getDvfinalvalidation()) {
                    daps.add(dap);
                }
            }
        }
        return daps;
    }
/**
 * get departme,t of a DAP version
 * @param dapV
 * @return 
 */
    public Department getCurrentDept(Dapversion dapV) {
        Department res = null;
        ArrayList<Decision> currentD = getAllCurrentDecision();
        for (Decision decision : currentD) {
            if (decision.getDapversion().equals(dapV)) {
                res = decision.getStepvalidation().getDepartment();
            }
        }
        System.out.println("Departement : " + res);
        return res;
    }
/**
 * get the last modification of a DAP version
 * @param dapV
 * @return 
 */
    public String getLastModification(Dapversion dapV) {
        String res = "There have been no modifications on this step as of now";
        ArrayList<Decision> currentD = getAllCurrentDecision();
        for (Decision decision : currentD) {
            if (decision.getDapversion().equals(dapV)) {
                List<Docversion> docs = getAllDocVersion();
                for (Docversion docv : docs) {
                    if (decision.equals(docv.getDecision())) {
                        res = docv.getDocuploaddate().toString();
                    }

                }
            }
        }

        return res;
    }
/**
 * get all Doc version from database
 * @return 
 */
    public List<Docversion> getAllDocVersion() {
        Query query = session.getNamedQuery("HQL_ALL_DOCVERSION_ORDERED");
        List result = query.list();
        return result;
    }
/**
 * get all decision in progress
 * @return 
 */
    public ArrayList<Decision> getAllCurrentDecision() {
        ArrayList<Decision> result = new ArrayList<>();
        List<Decision> lstAllDecision = session.getNamedQuery("HQL_ALL_DECISION").list();
        for (Decision decision : lstAllDecision) {
            if (decision.getDstate().equals("In progress")) {
                result.add(decision);
            }
        }
        return result;
    }
/**
 * get all DAP version from database
 * @return 
 */
    public List<Dapversion> getAllDapversions() {
        Query query = session.getNamedQuery("HQL_ALL_DAPVERSION");
        List result = query.list();
        return result;
    }
/**
 * get Seller by its mail
 * @param mail
 * @return 
 */
    public Seller getSellerByemail(String mail) {
        return (Seller) session.getNamedQuery("HQL_ALL_SELLER_BY_MAIL").setParameter("mail", mail).uniqueResult();

    }
/**
 * get all DAP version non validated
 * @return 
 */
    public List<Dapversion> getAllDAPinprogress() {
        return session.getNamedQuery("HQL_DAPVERSION_INVALIDATED").list();
    }

    public static void main(String[] args) {
        SellerCore core = new SellerCore();
//        core.getAllCurrentDecision();
//        System.out.println("MAIN  : " + core.getAllCurrentDecision());
//        Dapversion dv = new Dapversion();
//
//        List<Dapversion> lstres = core.getAllDapversions();
//
//        for (Dapversion dap : lstres) {
//            core.getLastModification(dap);
//            System.out.println("date dernière modif du dap " + dap.getDvidvtitle() + " : " + core.getLastModification(dap));
//        }

        System.out.println(core.getAllDAPinprogress());
        List<Dapversion> inprogress = new ArrayList<Dapversion>();
        inprogress = core.getAllDAPinprogress();
        List<Department> dpt = new ArrayList<Department>();
        List<String> dates = new ArrayList<String>();
        for (Dapversion dapversion : inprogress) {
            dpt.add(core.getCurrentDept(dapversion));
            dates.add(core.getLastModification(dapversion));
        }
        System.out.println(dpt);
        System.out.println(dates);

    }

}
